package com.ayuku.commons.ui.usergroup;

import java.util.List;
import java.util.logging.Logger;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableItem;

import com.ayuku.commons.AYConst;
import com.ayuku.commons.ListFormMaster;
import com.ayuku.commons.Util;
import com.ayuku.commons.business.appgroup.AppGroupImpl;
import com.ayuku.retail.persistence.model.AppGroup;

/**
 * 
 * @author putu kusuma <putukn@gmail.com>
 *
 */
public class ListAppGroup extends ListFormMaster {
	private static Logger LOG = Logger.getLogger(ListAppGroup.class.getName());

	private FormAppGroup frm;

	public ListAppGroup(Shell parent, int style) {
		super(parent, style);
	}

	@Override
	public void showEditForm() {
		TableItem[] selection = tableData.getSelection();
		if ((selection != null) && (selection.length > 0)) {
			TableItem itm = selection[0];
			LOG.info("FORM KEY: " + itm.getText(0));
			frm = new FormAppGroup(getDialogShell(), SWT.NULL,
					AYConst.MODE_VIEW);
			frm.setFormKey(itm.getText(0));
			frm.setFuncId(getFuncId());
			frm.open();
		} else {
			Util.showMsgBox(AYConst.FAIL_VIEW, AYConst.MSG_WARN,
					getDialogShell());
		}
	}

	@Override
	public void loadSearchData() throws Exception {
		populateTable("%" + getFSearch().getText() + "%");
	}

	@Override
	public void loadAllData() throws Exception {
		populateTable("%%");
	}

	@Override
	public void showAddForm() {
		frm = new FormAppGroup(getDialogShell(), SWT.NULL, AYConst.MODE_ADD);
		frm.setFuncId(getFuncId());
		frm.open();
	}

	@Override
	public void populateTable(String arg) throws Exception {
		tableData.removeAll();
		List<AppGroup> lst = AppGroupImpl.getList(arg);

		tableData.setItemCount(lst.size());
		data = new Object[lst.size()][];
		for (int i = 0; i < lst.size(); i++) {
			AppGroup pj = lst.get(i);
			data[i] = new Object[] { pj.getId(), (i + 1),  pj.getId(), pj.getName()};
		}
	}

	@Override
	public void additionalProcess() {
		getDialogShell().setText("Data Group");
		getDialogShell().setSize(580, 500);
		Util.setCenter(getDialogShell());
	}

	@Override
	public void printPreview() {
	}

	@Override
	public String[] getColumCaption() {
		final String[] titles = { "","No","Kode", "Deskripsi" };
		return titles;
	}

	@Override
	public int[] getColumWidth() {
		int[] i = {0,40, 150, 400 };
		return i;
	}

	@Override
	public Integer doDelete(List<String> lst) throws Exception {
		return AppGroupImpl.delete(lst);
	}



	@Override
	protected void fillTable(Object[] obj, TableItem item) {
		item.setText(0, Util.nullToString(obj[0]));
		item.setText(1, Util.nullToString(obj[1]));
		item.setText(2, Util.nullToString(obj[2]));
		item.setText(3, Util.nullToString(obj[3]));
	}
}
